package com.bgds.sys.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bgds.sys.common.pagevo.ElectricMotorVo;
import com.bgds.sys.common.pagevo.RollerVo;
import com.bgds.sys.common.pagevo.SectionVo;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.influx.service.InfluxService;
import com.bgds.sys.module.entity.AlarmLog;
import com.bgds.sys.module.entity.Device;
import com.bgds.sys.module.entity.DeviceDynamicAttr;
import com.bgds.sys.module.service.IAlarmLogService;
import com.bgds.sys.module.service.IDeviceDynamicAttrService;
import com.bgds.sys.module.service.IDeviceService;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author Smiling
 * @description: 辊轮监测
 * @date 2021/3/21  11:14
 */
@Slf4j
@Component
public class WheelJob implements Job {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IDeviceDynamicAttrService deviceDynamicAttrService;
    @Autowired
    private InfluxService influxService;
    @Autowired
    private IAlarmLogService alarmLogService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        //log.info( "编码:" +dataMap.get("deviceCode") );
        String deviceCode=dataMap.get("deviceCode").toString();
        Map<String,Object> data=getWheelData(deviceCode);
        String id=dataMap.get("id").toString();
        messagingTemplate.convertAndSendToUser(id,"/wheel",new ResultUtil<>().setData(data));
    }

    public Map<String,Object> getWheelData(String deviceCode){
        Map<String,Object> map=new HashMap<>(10);
        //获取辊道段下辊轮
       // List<DeviceDynamicAttr> deviceDynamicAttrs=deviceDynamicAttrService.listByDeviceCode(deviceCode,"curr");
        List<DeviceDynamicAttr> deviceDynamicAttrs=deviceDynamicAttrService.listByDeviceCode(deviceCode,null);
        if (CollectionUtil.isEmpty(deviceDynamicAttrs)){
            log.info("{}数据为空",deviceCode);
            return CollectionUtil.newHashMap();
        }
        List<Map<String,Object>> maps=influxService.queryByForMap(deviceCode,deviceDynamicAttrs);

        //封装辊道段下辊轮电机可视化图形数据
        SectionVo sv=new SectionVo();
        Device device=deviceService.getOneByCode(deviceCode);
        sv.setTitle(device.getName());
        //通过日志判断是否异常
        Integer state=alarmLogService.decideDeviceLevel(device.getDeviceCode());
        sv.setState(state);
        for(int i=0;i<deviceDynamicAttrs.size();i++){
            ElectricMotorVo ev=new ElectricMotorVo();
            DeviceDynamicAttr attr=deviceDynamicAttrs.get(i);
            for(Map<String,Object> m:maps){
                m.forEach((key,value)->{
                    if(!key.equals("time")){
                        setState(attr,value,ev);
                    }
                });
            }
            ev.setKey(i);
            sv.getNodes().add(ev);
        }
        map.put("deviceDynamicAttrs",deviceDynamicAttrs);
        map.put("sectionVo",sv);
        return map;
    }
   //设置辊轮的状态
    private void setState(DeviceDynamicAttr attr,Object value,ElectricMotorVo ev){
        if (StringUtils.isBlank(String.valueOf(value))){
            log.info("{},状态为空",attr.getDeviceCode());
            return;
        }
        Float f=  Float.valueOf(value.toString());
        if(f>attr.getOneTopLimit() && f<attr.getTwoTopLimit()){
            ev.setState(1);
        }else if(f>attr.getTwoTopLimit() && f<attr.getThreeTopLimit()){
            ev.setState(1);
        }else if(f>attr.getThreeTopLimit()){
            ev.setState(2);
        }
        else if(f>attr.getThreeLowLimit() && f<attr.getTwoLowLimit()){
            ev.setState(1);
        }else if(f>attr.getTwoLowLimit() && f< attr.getOneLowLimit()){
            ev.setState(1);
        }else if(f<attr.getThreeLowLimit()){
            ev.setState(2);
        }else{
            ev.setState(0);
        }
    }

}